package DRCOMM;
import java.io.*;
import java.util.*;

public class file_reader{

    /**
     * 读取文件为 List<String>
     * @param filename
     * @return
     */
    public static List<String> read_file(String filename){
        List<String> contents=new ArrayList<String>();
        try {
            File f=new File(filename);
            if(f.exists()){
                InputStreamReader isr = new InputStreamReader(new FileInputStream(filename), "UTF-8");
                BufferedReader in = new BufferedReader(isr);
                // BufferedReader in = new BufferedReader(new FileReader(filename));
                String str;
                while ((str = in.readLine()) != null) {
                    contents.add(str);
                }
                in.close();
            }else{
                InputStream is=file_reader.class.getResourceAsStream(filename.substring(1));
                BufferedReader in=new BufferedReader(new InputStreamReader(is,"UTF-8"));
                String str;
                while ((str = in.readLine()) != null) {
                    contents.add(str);
                }
                is.close();
            }
        } catch (IOException e) {
            contents=new ArrayList<String>();
        }
        return contents;
    }

    /**
     * 读取文件为 String 
     * @param filename
     * @return
     */
    public static String read_file2string(String filename){
        String contents="";
        try {
            File f=new File(filename);
            if(f.exists()){
                InputStreamReader isr = new InputStreamReader(new FileInputStream(filename), "UTF-8");
                BufferedReader br = new BufferedReader(isr);
                String str;
                while ((str = br.readLine()) != null) {
                    contents+=str+"\n";
                }
                br.close();
            }else{
                try {
                    contents=file_reader.read_jar_file2string(filename);
                } catch (Exception e) {
                }
            }
        } catch (IOException e) {
        }
        return contents;
    }

    /**
     * 读取JAR内的文件为 String 
     * @param filename
     * @return
     */
    public static String read_jar_file2string(String filename){
        filename=filename.substring(1);
        String contents;
        try {
            InputStream is=file_reader.class.getResourceAsStream(filename);
            BufferedReader in=new BufferedReader(new InputStreamReader(is,"UTF-8"));
            String str;
            contents="";
            while ((str = in.readLine()) != null) {
                contents+=str+"\n";
            }
            is.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
            contents=null;
        }
        return contents;
    }

    /**
     * 读取文件为 bytes
     * @param filename
     * @return
     */
    public static byte[] read_file2bytes(String filename){
        byte[] contents=null;
        try {
            File f=new File(filename);
            try (FileInputStream in = new FileInputStream(f)){
                contents = new byte[(int) f.length()];
                in.read(contents);
                in.close();
            }catch(Exception file_e){
                try {
                    // 由于InputStream.read可能出现一次没有读完的情况，需要通过循环判断是否读完所有数据
                    // 2023年5月7日 罗继东
                    InputStream is=file_reader.class.getResourceAsStream(filename.substring(1));
                    int bufferlength=is.available();
                    contents = new byte[bufferlength];
                    // byte[] buff;
                    int data_index = 0;
                    int tempLength;
                    do {
                        tempLength = is.read(contents, data_index, bufferlength-data_index);
                        if (tempLength > 0)
                            data_index += tempLength;
                    }
                    while (tempLength > 0);
                    // contents=buff;
                    // is.read(contents);
                    is.close();
                } catch (Exception jar_e) {
                    System.out.println(jar_e.getMessage());
                }
            } 
        } catch (Exception e) {
            System.out.println(e.getMessage());
            contents=null;
        }
        return contents;
    }


}